package com.frxuexi.park2302.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.UserBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.UserEntity
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, UserIntent, UserViewModel>() {
    override val binding: ActivityLoginBinding by lazy { ActivityLoginBinding.inflate(layoutInflater) }
    override val vm: UserViewModel by viewModels<UserViewModel>()


    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        val bean = it.data as UserBean
                        ToastUtils.showLong("登录成功")
                        SPUtils.getInstance().apply {
                            put(Const.TOKEN, bean.token)
                            put(Const.ROLE, bean.roleid)
                            put(Const.UID, bean.id)
                            put(Const.UNAME, bean.username)
                        }
//                        val entity = it.data as UserEntity
//                        SPUtils.getInstance().put(Const.ROLE, entity.role)
//                        Log.i("===",entity.toString())
                        start()
                    }

                    is UiState.onError -> {
                        Log.i("lr", it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    fun start() {
//        val role = SPUtils.getInstance().getInt(Const.ROLE, -1)
//        if (role == 19) {
        if(SPUtils.getInstance().getString(Const.TOKEN,"").isNotEmpty()) {
            ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        }
//        } else if (role == 20) {
//            ARouter.getInstance().build("/app/RegisterActivity").navigation()
//            finish()
//        }
    }

    override fun initView() {
        start()

        binding.btnLogin.setOnClickListener {
            val uname = binding.loginUname.text.toString()
            val pass = binding.loginPass.text.toString()
            vm.sendIntent(UserIntent.login(uname, pass))
//            vm.sendIntent(UserIntent.loginRoom(uname, pass))
        }

        binding.loginRegister.setOnClickListener {
            ARouter.getInstance().build("/app/RegisterActivity").navigation()
        }
    }
}